package ar.com.mindset.geometrics.legends {
	import ar.com.mindset.geometrics.ColorEntry;
	import ar.com.mindset.geometrics.RegionsLegend;

	import com.degrafa.GeometryGroup;
	import com.degrafa.GraphicText;
	import com.degrafa.geometry.Geometry;
	import com.degrafa.geometry.Line;
	import com.degrafa.geometry.RegularRectangle;
	import com.degrafa.paint.SolidFill;
	import com.degrafa.paint.SolidStroke;

	import flash.display.Graphics;
	import flash.geom.Rectangle;

	import mx.collections.ArrayCollection;
	import mx.formatters.NumberFormatter;

	public class BarLegend extends GeometryGroup implements RegionsLegend {

		public function BarLegend(x:Number=350, y:Number=350, width:Number=350) {
			super();
			this.x=x;
			this.y=y;
			this.width=width;
		}


		public function initialize(divisions:ArrayCollection, maxColor:Object):void {

			/*
			   first value
			   var graphicText:GraphicText = new GraphicText();
			   graphicText.antiAliasType="advanced";
			   graphicText.autoSizeField=true;
			   graphicText.fontSize=10;
			   graphicText.embedFonts=true;
			   graphicText.fontFamily="ArialEmbedded";
			   graphicText.x=-5;
			   graphicText.y=0;
			 graphicText.text="0";	*/

			for each (var geo:*in this.geometry) {
				geo.visible=false
			}

			var collection:ArrayCollection=new ArrayCollection();

			var referenceWidth:Number=this.width / (divisions.length + 1);
			var i:int=0;
			for each (var element:ColorEntry in divisions) {
				var numberFormatter:NumberFormatter=new NumberFormatter();
				numberFormatter.precision=2;
				var formatedKey:String=numberFormatter.format(element.key);
				addColorReference(referenceWidth * i, referenceWidth, element.color, formatedKey, collection, i % 2 == 0);
				i++;
			}

			addRectangle(referenceWidth * i, referenceWidth, maxColor, collection);

			this.geometry=collection.toArray();
		}

		private function addRectangle(x:Number, width:Number, color:Object, geometryCollection:ArrayCollection):void {
			var legendItemOutline:SolidStroke=new SolidStroke("#000000", 0.5);

			var regularRectangle:RegularRectangle=new RegularRectangle(x, 20, width, 12);
			regularRectangle.stroke=legendItemOutline;
			regularRectangle.fill=new SolidFill(color);

			geometryCollection.addItem(regularRectangle)
		}


		private function addColorReference(x:Number, width:Number, color:Object, upperBound:String, geometryCollection:ArrayCollection, odd:Boolean=true):void {
			var graphicText:GraphicText=new GraphicText();
			graphicText.antiAliasType="advanced";
			graphicText.autoSizeField=true;
			graphicText.fontSize=10;
			graphicText.embedFonts=true;
			graphicText.color = 0;
			graphicText.fontFamily="ArialEmbedded";
			graphicText.text=upperBound;

			graphicText.x=x + width - 5;
			if (odd) {
				graphicText.y=37;
			} else {
				graphicText.y=0;
			}



			var legendItemOutline:SolidStroke=new SolidStroke("#000000", 0.5);

			var line:Line;
			if (odd) {
				line=new Line(x + width, 32, x + width, 38);
			} else {
				line=new Line(x + width, 15, x + width, 20);
			}
			line.stroke=legendItemOutline;

			geometryCollection.addItem(graphicText);
			geometryCollection.addItem(line)

			addRectangle(x, width, color, geometryCollection);
		}

		override public function draw(graphics:Graphics, rc:Rectangle):void {
			super.draw(graphics, rc);
		}

	}
}